VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "CTimer"
Attribute VB_GlobalNameSpace = True
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = True
Option Explicit
Private idTimer As Long
Public Event TimerEvent()

' Start the timer object, if not already running.
Public Function StartTimer(ByVal interval As Long) As Boolean
Attribute StartTimer.VB_Description = "Start a timer associated with the CTimer object. The return value indicated the success or failure of starting the Window's timer."
  If idTimer <> 0 Then
    ' Don't start timer if aleady started.
    StartTimer = False
    Exit Function
  End If
  ' Create new Window's timer and specify the global
  ' TimerProc function as the callback.
  idTimer = SetTimer(0, 0, interval, AddressOf TimerProc)
  If idTimer <> 0 Then
    ' Timer creation succeeded. Add this object to the global collection
    ' of objects with active timers.
    AddTimer Me
    StartTimer = True
  Else
    StartTimer = False
  End If
End Function

' Kill the active timer and clean up. This allows this timer
' object to be reused simply by allowing the client to re-invoke
' the StartTimer method.
Public Sub StopTimer()
Attribute StopTimer.VB_Description = "Stop the timer associated with the CTimer object"
  KillTimer 0, idTimer
  
  ' Remove the timer from the global collection. You must reset the
  ' timer ID AFTER removal because the id is used in the RemoveTimer
  ' function.
  RemoveTimer Me
  idTimer = 0
End Sub

Private Sub Class_Terminate()
  ' Be sure that if a timer is active that it is killed before this
  ' object is destroyed.
  If idTimer <> 0 Then
    KillTimer 0, idTimer
  End If
End Sub

Friend Property Get TimerID() As String
  ' The TimerID property is used by the standard module to retrieve
  ' this object's collection key, which is of type String. Accordingly,
  ' this property is of type String.
  TimerID = Str$(idTimer)
End Property

Friend Sub Timer_Event()
  ' This function is called by the TimerProc callback function in the
  ' context of the object that owns the timer that fired. Here, we simply
  ' raise the event to the client(s) of this timer object.
  RaiseEvent TimerEvent
End Sub
